//用户导入导出等
/**
 * @openapi
 * tags:
 * - name: admin/user
 *   description: 用户数据处理
 */

const router = require("express-promise-router")();
const multer  = require('multer');
const config=require("../../config/config");
const tmp_uploads_dir=config.tmp_uploads_dir;
const GoodService =require("../../services/goods")
const upload = multer({ dest: tmp_uploads_dir });

/**
 * @openapi
 * /teacher/good/add:
 *   post:
 *     tags:
 *     - admin/auto_borrow
 *     summary: 创建名单
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: string
 *               goods:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/add', async function(req, res) {
    let {goods,kindId}=req.body;
    const result=await GoodService.add(goods,kindId);
    return res.json(result);
});

router.post('/add_good', async function(req, res) {
    let {good}=req.body;
    const result=await GoodService.add_good(good);
    return res.json(result);
});
/**
 * @openapi
 * /teacher/good/remove:
 *   post:
 *     tags:
 *     - admin/auto_borrow
 *     summary: 删除物料
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/remove', async function(req, res) {
    let {ids}=req.body;
    const result=await GoodService.remove(ids);
    return res.json(result);
});
/**
 * @openapi
 * /teacher/good/change_owner:
 *   post:
 *     tags:
 *     - admin/auto_borrow
 *     summary: 修改物料持有人
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/change_owner', async function(req, res) {
    let {goodIds, user}=req.body;
    const result=await GoodService.change_owner(goodIds, user);
    return res.json(result);
});

/**
 * @openapi
 * /teacher/good/update:
 *   post:
 *     tags:
 *     - admin/auto_borrow
 *     summary: 更新
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/update', async function(req, res) {
    let {key,value, id}=req.body;
    const result=await GoodService.update(key,value, id);
    return res.json(result);
});
/**
 * @openapi
 * /teacher/good/change_owner_count:
 *   post:
 *     tags:
 *     - admin/auto_borrow
 *     summary: 修改持有数量
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/change_owner_count', async function(req, res) {
    let {list, user,type}=req.body;
    const result=await GoodService.change_owner_count(list, user,type);
    return res.json(result);
});

/**
 * @openapi
 * /teacher/good/save_record:
 *   post:
 *     tags:
 *     - admin/auto_borrow
 *     summary: 保存借用流水
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               usernames:
 *                  type: array
 *                  items:
 *                      type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回创建的名单
 */
router.post('/save_record', async function(req, res) {
    let {list, user, type}=req.body;
    const result=await GoodService.save_record(list, user,type);
    return res.json(result);
});

/**
 * @openapi
 * /teacher/good/import/check_file:
 *   post:
 *     tags:
 *     - name_list
 *     summary: 导入物料第一步，检查excel文件
 *     requestBody:
 *       content:
 *         multipart/form-data:
 *           schema:
 *             type: object
 *             properties:
 *               file:
 *                 type: string
 *                 format: binary
 *               nameRowNumber:
 *                 type: number
 *               firstDataRowNumber:
 *                 type: number
 *               fieldsMapping:
 *                 type: object  例如："{\"学号\":\"username\",\"姓名\":\"showName\",\"专业\":\"subject\",\"班级\":\"eduClass\"}"
 *     responses:
 *       200:
 *         description: 返回JSON文件信息
 */
router.post('/import/check_file', upload.single('file'), async function  (req, res){
    let {nameRowNumber=1,firstDataRowNumber=2,fieldsMapping}=req.body;
    if(fieldsMapping){
        fieldsMapping=JSON.parse(fieldsMapping);
    }
    //检查Excel文件是否合乎格式
    const result=await GoodService.checkExcelFile(req.file.path,nameRowNumber,firstDataRowNumber,fieldsMapping);
    if(result.error){
        return res.status(400).send(result);
    }else{
        return res.json(result);
    }
});


/**
 * @openapi
 * /teacher/good/import/confirm_file:
 *   post:
 *     tags:
 *     - name_list
 *     summary: 导入物料第二步，确认导入excel文件
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               filePath:
 *                 type: string
 *               nameRowNumber:
 *                 type: number
 *               firstDataRowNumber:
 *                 type: number
 *               fieldsMapping:
 *                 type: object
 *               id:
 *                 type: string
 *       required: true
 *     responses:
 *       200:
 *         description: 返回有几条重复，几条新增
 */
router.post('/import/confirm_file',  async function  (req, res){
    //从客户端取得要导入的文件路径，由check_file给出
    var default_nameRowNumber = 1;
    var default_firstDataRowNumber = 2;
    let {nameRowNumber=default_nameRowNumber,firstDataRowNumber=default_firstDataRowNumber,filePath,fieldsMapping,kindId}=req.body;
    // if(fieldsMapping){
    //     fieldsMapping=JSON.parse(fieldsMapping);
    // }
    //导入到数据库
    const result=await GoodService.importExcelFile(filePath,nameRowNumber,firstDataRowNumber,fieldsMapping,kindId);
    res.json(result);
});





module.exports = router;
